An R package is a structured collection of R functions, documentation, and sometimes data sets, bundled together to make code reusable and shareable. Instead of copying and pasting functions between scripts, packages allow you to organize your work in one place, keep it version-controlled, and easily share it with others.
Using packages ensures consistency, efficiency, and reproducibility—whether you are working on your own or collaborating in a team.
Our methodology consists of different steps:
These steps are the same no matter the elimination campaign we’re working on. And each step can be accompanied by some standard visualizations that also don’t change no matter the campaign. In order to avoid having to code the same thing to plot the same figure all the time, we can gather all the functions needed in a package that we will load and it will produce all the plots.
An R package is essentially a folder with a specific structure that R understands. The main components are:
R/ : This folder contains all your function scripts.
DESCRIPTION : A file with metadata about the package (name, version, author, dependencies).
NAMESPACE : A file that tells R which functions to export and which packages you depend on (this is usually handled automatically by roxygen2).
man/ : Documentation files for each function (auto-generated).
install.packages("devtools")
install.packages("roxygen2")
devtools::create("myPackage")
Add your .R scripts into R/
Document by using roxygen comments above the functions
#' Title of the function
#' @param x Description of parameter x
#' @return What the function returns
my_function <- function(x) {
x + 1
}
devtools::document()
devtools::install()